//
//  Download.m
//  eBook
//
//  Created by Hai Le Dang on 8/30/11.
//  Copyright 2011 CMCSoft. All rights reserved.
//

#import "DownloadHelper.h"
#import "ASIHTTPRequest.h"
//#import "Constants.h"

@implementation DownloadHelper
@synthesize method;
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)downloadFile:(NSString*)fileName toFolderWithName:(NSString*)folderName withURL:(NSURL*)url andNotification:(NSString*)notifyName
{
    NSFileManager *fm = [[NSFileManager alloc] init];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *optionPath = [documentPath stringByAppendingPathComponent:folderName];
    if (![fm fileExistsAtPath:optionPath]) {
        [fm createDirectoryAtPath:optionPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *tempDownloadPath = [documentPath stringByAppendingPathComponent:@"TempDownload"];
    if (![fm fileExistsAtPath:tempDownloadPath]) {
        [fm createDirectoryAtPath:tempDownloadPath withIntermediateDirectories:YES attributes:nil error:nil];
    }

    notificationName=[notifyName retain];
    
    _request = [[ASIHTTPRequest alloc] initWithURL:url];
    [_request setUseSessionPersistence:YES];
    [_request setValidatesSecureCertificate:NO];
    [_request setDownloadDestinationPath:[optionPath stringByAppendingPathComponent:fileName]];
    
    [_request setDelegate:self];
    
    [_request setTemporaryFileDownloadPath:[tempDownloadPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.download",fileName]]];
    
    [_request setAllowResumeForFileDownloads:YES];
    
    [_request startAsynchronous];
}

- (void)downloadFile:(NSString*)fileName url:(NSURL*)url progressView:(UIProgressView *)progressView
{
//    NSLog(@"downloading:%@",url);
    _request = [[ASIHTTPRequest alloc] initWithURL:url];
//    [self setMethod:DOWNLOAD_IMAGE];
    [_request setUseSessionPersistence:YES]; //Shouldn't be needed
    [_request setValidatesSecureCertificate:NO];    // Shouldn't be, but in case of SSL, we need to bypass the CA
    [_request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:fileName]];

    if (progressView != nil) {
        [_request setDownloadProgressDelegate:progressView];
    }

    [_request setDelegate:self];
    NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *tempDownloadPath = [documentPath stringByAppendingPathComponent:@"TempDownload"];
    if (![fm fileExistsAtPath:tempDownloadPath]) {
        [fm createDirectoryAtPath:tempDownloadPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    [_request setTemporaryFileDownloadPath:[tempDownloadPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.download",fileName]]];

    [_request setAllowResumeForFileDownloads:NO];
    
    [_request startAsynchronous];
}

- (void)downloadFile:(NSString*)fileName withURL:(NSURL*)url andNotificationName:(NSString*)_notificationName
{
    notificationName=[_notificationName retain];
    
    _request = [[ASIHTTPRequest alloc] initWithURL:url];
    [_request setUseSessionPersistence:YES];
    [_request setValidatesSecureCertificate:NO];
    [_request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:fileName]];
    
    [_request setDelegate:self];
    
    
    NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *tempDownloadPath = [documentPath stringByAppendingPathComponent:@"TempDownload"];
    
    if (![fm fileExistsAtPath:tempDownloadPath]) {
        [fm createDirectoryAtPath:tempDownloadPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    [_request setTemporaryFileDownloadPath:[tempDownloadPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.download",fileName]]];
    
    [_request setAllowResumeForFileDownloads:NO];
    
    [_request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if(method == DOWNLOAD_IMAGE)
    {
        
    }
    if (method== DOWNLOAD_IMAGE_AT_IMAGEVIEWDETAIL) {
        [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:[request.url lastPathComponent]];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"failrequest" object:error];
    
    if (method== DOWNLOAD_IMAGE_AT_IMAGEVIEWDETAIL) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:[notificationName stringByAppendingString:@"_FAIL"] object:[request.url lastPathComponent]];
    }
}

- (void)dismissRequest
{
    // Cancels an asynchronous request
    [_request cancel];
    // Cancels an asynchronous request, clearing all delegates and blocks first
    [_request clearDelegatesAndCancel];
}

- (void)dealloc
{
    [super dealloc];
}
@end
